//
//  TRMessageCell.m
//  TLBS
//
//  Created by tarena on 14-4-4.
//  Copyright (c) 2014年 tarena. All rights reserved.
//

#import "TRMessageCell.h"

@implementation TRMessageCell

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

            self.myImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        [self addSubview:self.myImageView];
    }
    return self;
}


#define CELL_MARGIN_TB      4.0     //气泡上下外边距
#define CELL_MARGIN_LR      10.0    //气泡左右外边距

#define CELL_CORNER         18.0    //气泡圆角半径
#define CELL_TAIL_WIDTH     16.0    //气泡尾巴宽度
#define CELL_CORNER_WIDTH   18      //气泡圆角半径

#define MAX_WIDTH_OF_TEXT   200.0   //文字限制宽度
#define CELL_PADDING        8.0     //气泡内边距（距离文本）


-(void)layoutSubviews
{
    [super layoutSubviews];

    
    
   
        
       
    self.label.text = self.message.body;
    
     if ([self.message.type isEqualToString:@"message"]) {
     }else if ([self.message.type isEqualToString:@"image"]){
          if (![self.message from]) {//自己发
         self.label.text = @"发送图片，点击查看";
          }else{
              self.label.text = @"接受到图片，点击查看";
          }
    
     }else{//发送的是音频
         if (![self.message from]) {//自己发
             self.label.text = @"发送音频，点击收听";
         }else{
             self.label.text = @"接受到音频，点击收听";
         }
     }
    
    if (![self.message from]) {//自己发
        
        self.label.textColor = [UIColor whiteColor];
        
        UIImage * image = [UIImage imageNamed:@"huihua2.png"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(CELL_CORNER, CELL_CORNER, CELL_CORNER, CELL_CORNER + CELL_TAIL_WIDTH)];
        self.popImageView.image = image;
        
        CGRect rectOfText =
        CGRectMake(0, 0, MAX_WIDTH_OF_TEXT, 9999.9);
        rectOfText = [self.label textRectForBounds:rectOfText limitedToNumberOfLines:0];
        
        CGRect frameOfLabel = CGRectZero;
        frameOfLabel.size = rectOfText.size;
        frameOfLabel.origin.y = CELL_MARGIN_TB + CELL_PADDING;
        frameOfLabel.origin.x =
        self.bounds.size.width - CELL_MARGIN_LR - CELL_TAIL_WIDTH
        - CELL_PADDING - frameOfLabel.size.width;
        self.label.frame = frameOfLabel;
        
        CGRect frameOfPop = frameOfLabel;
        frameOfPop.origin.x -= CELL_PADDING;
        frameOfPop.origin.y -= CELL_PADDING;
        frameOfPop.size.width += CELL_PADDING * 2 + CELL_TAIL_WIDTH;
        frameOfPop.size.height += CELL_PADDING * 2;
        self.popImageView.frame = frameOfPop;
        
        CGRect bounds = self.bounds;
        bounds.size.height = frameOfPop.size.height + CELL_MARGIN_TB * 2;
        self.bounds = bounds;
        
    } else {//接收到的消息
        
        self.label.textColor = [UIColor darkGrayColor];
        
        UIImage * image = [UIImage imageNamed:@"huihua1.png"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(CELL_CORNER, CELL_CORNER + CELL_TAIL_WIDTH, CELL_CORNER, CELL_CORNER)];
        self.popImageView.image = image;
        
        CGRect rectOfText =
        CGRectMake(0, 0, MAX_WIDTH_OF_TEXT, 9999.9);
        rectOfText = [self.label textRectForBounds:rectOfText limitedToNumberOfLines:0];
        
        CGRect frameOfLabel = CGRectZero;
        frameOfLabel.size = rectOfText.size;
        frameOfLabel.origin.y = CELL_MARGIN_TB + CELL_PADDING;
        frameOfLabel.origin.x = CELL_MARGIN_LR + CELL_TAIL_WIDTH + CELL_PADDING;
        self.label.frame = frameOfLabel;
        
        CGRect frameOfPop = frameOfLabel;
        frameOfPop.origin.x -= (CELL_PADDING + CELL_TAIL_WIDTH);
        frameOfPop.origin.y -= CELL_PADDING;
        frameOfPop.size.width += CELL_PADDING * 2 + CELL_TAIL_WIDTH;
        frameOfPop.size.height += CELL_PADDING * 2;
        self.popImageView.frame = frameOfPop;

        CGRect bounds = self.bounds;
        bounds.size.height = frameOfPop.size.height + CELL_MARGIN_TB * 2;
        self.bounds = bounds;
    }
    
}
//显示Cell的时候 和被用户点击的时候调用此方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    //如果cell点击
    if (selected) {
        if ([self.message.type isEqualToString:@"image"]) {
            UIImageView *iv = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
            NSData *data = [[NSData alloc]initWithBase64EncodedString:self.message.body options:0];
            
            UIImage *image = [UIImage imageWithData:data];
            iv.image = image;
            [iv setContentMode:UIViewContentModeScaleAspectFit];
            UIViewController *vc = [[UIViewController alloc]init];
            [vc.view setBackgroundColor:[UIColor whiteColor]];
            [vc.view addSubview:iv];
            [self.delegate.navigationController pushViewController:vc animated:YES];
        }else if ([self.message.type isEqualToString:@"audio"]){
            NSData *audioData = [[NSData alloc]initWithBase64EncodedString:self.message.body options:0];
            
            self.player = [[AVAudioPlayer alloc]initWithData:audioData error:nil];
            [self.player play];
            
        }
    }
}


@end
